package com.shopx.gw.config;

import com.shopx.gw.interceptor.ShopxInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author lituo
 * @className: BuildConfig
 * @description: TODO
 * @date 2021/12/2 2:05 PM
 */
@Configuration
@Slf4j
public class ShopxConfig implements WebMvcConfigurer {

  @Value("${file.oss.localPath}")
  private String localPath;

  @Value("${file.oss.filePrefix}")
  private String filePrefix;

  @Bean
  public ShopxInterceptor shopxInterceptor() {
    return new ShopxInterceptor();
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(shopxInterceptor()).addPathPatterns("/**");
  }

  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
        .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
        .allowedOrigins("*")
        .allowedHeaders("*");
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    log.info("configPath:{},localConfigPath:{}",filePrefix,localPath);
    registry.addResourceHandler(filePrefix+"/**")
        .addResourceLocations("file:" +localPath);
  }

}
